{% extends "base.html" %}

{% block content %}
<div class="container-fluid">
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="mb-0">
                <i class="fas fa-history me-2"></i>{{ pipe.name }} - 维护记录
            </h5>
            <div>
                <a href="{{ url_for('network.maintenance_add', pipe_id=pipe.id) }}" class="btn btn-success">
                    <i class="fas fa-plus me-1"></i>添加记录
                </a>
                <a href="{{ url_for('network.network_index') }}" class="btn btn-secondary">
                    <i class="fas fa-arrow-left me-1"></i>返回
                </a>
            </div>
        </div>
        <div class="card-body">
            <!-- 搜索表单 -->
            <form method="get" class="mb-4">
                <div class="row g-3">
                    <div class="col-md-3">
                        <label class="form-label">维护类型</label>
                        <select name="maintenance_type" class="form-select">
                            <option value="">全部</option>
                            <option value="check" {% if search_type == 'check' %}selected{% endif %}>检查</option>
                            <option value="repair" {% if search_type == 'repair' %}selected{% endif %}>维修</option>
                            <option value="replace" {% if search_type == 'replace' %}selected{% endif %}>更换</option>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">维护状态</label>
                        <select name="status" class="form-select">
                            <option value="">全部</option>
                            <option value="completed" {% if search_status == 'completed' %}selected{% endif %}>已完成</option>
                            <option value="in_progress" {% if search_status == 'in_progress' %}selected{% endif %}>进行中</option>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">开始日期</label>
                        <input type="date" name="start_date" class="form-control" value="{{ start_date if start_date }}">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">结束日期</label>
                        <input type="date" name="end_date" class="form-control" value="{{ end_date if end_date }}">
                    </div>
                    <div class="col-12">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-search me-1"></i>搜索
                        </button>
                        <a href="{{ url_for('network.maintenance_history', pipe_id=pipe.id) }}" class="btn btn-secondary">
                            <i class="fas fa-redo me-1"></i>重置
                        </a>
                    </div>
                </div>
            </form>
            
            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th>维护时间</th>
                            <th>维护类型</th>
                            <th>操作人员</th>
                            <th>维护状态</th>
                            <th>维护描述</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for record in records %}
                        <tr>
                            <td>{{ record.maintenance_date.strftime('%Y-%m-%d %H:%M') }}</td>
                            <td>
                                {% if record.maintenance_type == 'check' %}检查
                                {% elif record.maintenance_type == 'repair' %}维修
                                {% elif record.maintenance_type == 'replace' %}更换
                                {% else %}{{ record.maintenance_type }}{% endif %}
                            </td>
                            <td>{{ record.operator }}</td>
                            <td>
                                <span class="badge {% if record.status == 'completed' %}bg-success{% else %}bg-warning{% endif %}">
                                    {% if record.status == 'completed' %}已完成
                                    {% elif record.status == 'in_progress' %}进行中
                                    {% else %}{{ record.status }}{% endif %}
                                </span>
                            </td>
                            <td>{{ record.description }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <!-- 分页 -->
            {% if pagination and pagination.pages > 1 %}
            <nav aria-label="Page navigation" class="mt-4">
                <ul class="pagination justify-content-center">
                    <li class="page-item {% if not pagination.has_prev %}disabled{% endif %}">
                        <a class="page-link" href="{{ url_for('network.maintenance_history', pipe_id=pipe.id, page=pagination.prev_num, maintenance_type=search_type, status=search_status, start_date=start_date, end_date=end_date) if pagination.has_prev else '#' }}">
                            <i class="fas fa-chevron-left"></i>
                        </a>
                    </li>
                    {% for page in pagination.iter_pages() %}
                        {% if page %}
                            <li class="page-item {% if page == pagination.page %}active{% endif %}">
                                <a class="page-link" href="{{ url_for('network.maintenance_history', pipe_id=pipe.id, page=page, maintenance_type=search_type, status=search_status, start_date=start_date, end_date=end_date) }}">{{ page }}</a>
                            </li>
                        {% else %}
                            <li class="page-item disabled">
                                <span class="page-link">...</span>
                            </li>
                        {% endif %}
                    {% endfor %}
                    <li class="page-item {% if not pagination.has_next %}disabled{% endif %}">
                        <a class="page-link" href="{{ url_for('network.maintenance_history', pipe_id=pipe.id, page=pagination.next_num, maintenance_type=search_type, status=search_status, start_date=start_date, end_date=end_date) if pagination.has_next else '#' }}">
                            <i class="fas fa-chevron-right"></i>
                        </a>
                    </li>
                </ul>
            </nav>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %} 